package client_service_controller;

import java.awt.AWTException;
import java.awt.CheckboxMenuItem;
import java.awt.Font;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import client_listen_update_files.ConfigManager;

/**
 * Tray Icon GUI
 * @author Orit
 *
 */
public class TrayIconGUI {
	//--Attributes--//
	Image image = Toolkit.getDefaultToolkit().getImage("src/client_service_controller/mybox.png");
	final PopupMenu pop_up = new PopupMenu();
	final TrayIcon trayIcon = new TrayIcon(image, "drop box");
	final SystemTray tray = SystemTray.getSystemTray();
	MenuItem aboutItem = new MenuItem("About");
	MenuItem dropboxFolder = new MenuItem("Open my dropbox folder");
	MenuItem preferenceGUI = new MenuItem("Search by TAG");	
	MenuItem dropboxWebsite = new MenuItem("Launch my dropbox website");
	Menu serviceMenu = new Menu("Service");
	final CheckboxMenuItem startItem = new CheckboxMenuItem("Start");
	final CheckboxMenuItem stopItem = new CheckboxMenuItem("Stop");
	final CheckboxMenuItem continueItem = new CheckboxMenuItem("Continue");
	final CheckboxMenuItem pauseItem = new CheckboxMenuItem("Pause");
	MenuItem exitItem = new MenuItem("Exit");
	
	//--C'tor Tray_Icon_Gui--//
	/**
	 * Tray Icon GUI C'tor - start tray icon && all his listeners automatically 
	 */
	public TrayIconGUI() {		
		//Check the SystemTray is supported
		if (!SystemTray.isSupported()) {
		    System.out.println("SystemTray is not supported");
		    return;
		}
		//--we want to start the service automatically when tray icon(=service) is loaded:--//	
		setFontsToButtons();
		initiateServiceButtons();       //----set enable buttons at first run---//
		listnersToServiceButtons();
		listnerToExitButton();
		addComponentToMenu();
		
		if (!ConfigManager.get().hasUserInfo()){
			UserNPass.showGUI(trayIcon);
			
		}
		else{
			
			Executor.getInstance().startService(ConfigManager.get().getConfig().getUserName()
								,ConfigManager.get().getConfig().getPassword());
		}
		
		
	}

	private void addComponentToMenu() {
		// TODO Auto-generated method stub
		pop_up.add(aboutItem);
		pop_up.addSeparator();
		pop_up.add(dropboxFolder);
		pop_up.add(preferenceGUI);
		pop_up.add(dropboxWebsite);
		pop_up.addSeparator();
		pop_up.add(serviceMenu);
		serviceMenu.add(startItem);
		serviceMenu.add(stopItem);
		serviceMenu.add(continueItem);
		serviceMenu.add(pauseItem);
		pop_up.add(exitItem);	
		trayIcon.setPopupMenu(pop_up);
		try {
		    tray.add(trayIcon);
		} catch (AWTException e) {
		    System.out.println("TrayIcon could not be added.");
		}		
	}

	private void listnerToExitButton() {
	    exitItem.addActionListener (new ActionListener () { 
		       public void actionPerformed ( ActionEvent e ) { 
		    	   tray.remove(trayIcon);}});  						
	}

	private void listnersToServiceButtons() {
		//start button listener
	    startItem.addItemListener( new ItemListener () {		
		       @Override
		       public void itemStateChanged(ItemEvent arg0) {		    	   
		    	  
		    	   pauseItem.setEnabled(true);
		    	   continueItem.setEnabled(false);
		    	   startItem.setEnabled(false);
		    	   stopItem.setEnabled(true);
		    	   turnOffButtons(startItem,stopItem,continueItem,pauseItem);
		    	   startItem.setState(true);
		       } 
		    }) ;  		
	    
	    //stop button listener
	    stopItem.addItemListener(new ItemListener () { 		
		       @Override
		       public void itemStateChanged(ItemEvent arg0) {		    	  
		    	   Executor.getInstance().stopService();
		    	   pauseItem.setEnabled(false);
		    	   continueItem.setEnabled(false);
		    	   startItem.setEnabled(true);
		    	   stopItem.setEnabled(false); 
		    	   turnOffButtons(startItem,stopItem,continueItem,pauseItem);
		    	   stopItem.setState(true);
		       } 
		    }) ;  		

	    //continue button listener
	    continueItem.addItemListener(new ItemListener () { 
		       @Override
		       public void itemStateChanged(ItemEvent arg0) {		    	   
		    	   pauseItem.setEnabled(true);
		    	   continueItem.setEnabled(false);
		    	   startItem.setEnabled(false);
		    	   stopItem.setEnabled(true);
		    	   turnOffButtons(startItem,stopItem,continueItem,pauseItem);
		    	   continueItem.setState(true);	    	   
		       } 
		    }) ;  		

	    //pause button listener
	    pauseItem.addItemListener(new ItemListener () { 
		       @Override
		       public void itemStateChanged(ItemEvent arg0) {		    	  
		    	   pauseItem.setEnabled(false);
		    	   continueItem.setEnabled(true);
		    	   startItem.setEnabled(false);
		    	   stopItem.setEnabled(true);
		    	   turnOffButtons(startItem,stopItem,continueItem,pauseItem);
		    	   pauseItem.setState(true);
		       } 
		    }) ;  		
	}

	//sets the fonts for all the buttons
	private void setFontsToButtons(/*MenuItem aboutItem,
			MenuItem dropboxFolder, MenuItem dropboxWebsite, Menu serviceMenu, MenuItem exitItem*/) {
		aboutItem.setFont(new Font("Serif", Font.PLAIN, 14));
		dropboxFolder.setFont(new Font("Serif", Font.BOLD, 15));
		preferenceGUI.setFont(new Font("Serif", Font.PLAIN, 14));
		dropboxWebsite.setFont(new Font("Serif", Font.PLAIN, 14));
		serviceMenu.setFont(new Font("Serif", Font.PLAIN, 14));
		exitItem.setFont(new Font("Serif", Font.PLAIN, 14));
	}
	//set buttons when tray icon program starting to run
	private void initiateServiceButtons(){
		startItem.setEnabled(false);
		stopItem.setEnabled(true);
		pauseItem.setEnabled(true);
		continueItem.setEnabled(false);		
	}

	//remove v from all the buttons
	private void turnOffButtons(CheckboxMenuItem startItem,
		CheckboxMenuItem stopItem, CheckboxMenuItem continueItem,
		CheckboxMenuItem pauseItem) {		
		startItem.setState(false);
		stopItem.setState(false);
		continueItem.setState(false);
		pauseItem.setState(false);
	}

}

